
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Laurens
 */
public class characterEditor 
{
    characterList characters = new characterList();
    public characterEditor()
    {
        
    }
    
    /**
     * Sorts the vector in descending order of line numbers
     * @param lineNumbers
     * @return
     */
    public static Vector<Integer> insertion_sort(Vector<Integer> lineNumbers)
    {
        if(lineNumbers == null)
        {
            return new Vector<Integer>(null);
        }
        else
        {
            for (int i = 1; i < lineNumbers.size(); i++)
            {
                int j = i;
                int k = lineNumbers.get(i);
                while ((j > 0) && (lineNumbers.get(j-1) > k))
                {
                    lineNumbers.set(j, lineNumbers.get(j-1));
                    j--;
                }
                lineNumbers.set(j, k);
            }
            //returns the sorted line numbers
            return lineNumbers;
        }
    }

    /**
     * Removes duplicate values inside vector sortedLineNumbers
     * @param sortedLineNumbers
     * @return
     */
    public static Vector<Integer> removeDuplicates(Vector<Integer> sortedLineNumbers)
    {
        int i = 0;
        int j = 0;
        boolean duplicates = false;
        Vector<Integer> v = new Vector<Integer>();
        for (i = 0; i < sortedLineNumbers.size(); i++)
        {
            duplicates = false;
            for (j = (i + 1); j < sortedLineNumbers.size(); j++)
            {
                if (sortedLineNumbers.elementAt(i).toString().equalsIgnoreCase(sortedLineNumbers.elementAt(j).toString()))
                {
                    duplicates = true;
                }
            }
            if (duplicates == false)
            {
                v.add(sortedLineNumbers.elementAt(i));
            }
        }
        //returns none duplicate values of sortedLineNumbers
        return v;
    }
    
    /**
     * Searches the file for occurences of newMethod, if there is occurences then
     * the refactoring is not possible
     * @param newMethod
     * @param file
     * @return
     */
    public boolean searchNewValue(String newMethod, String file)
    {
        Vector<String> comment = new Vector<String>();
        comment.add("");
        comment.add("*");
        comment.add("**");
        comment.add("***");
        comment.add("****");
        comment.add("*****");
        comment.add("******");
        boolean value = false;
        int i = 0;
        Scanner scanner = new Scanner(file);
        Vector<String> characterList = characters.characterListMethod();
        while(scanner.hasNext())
        {
            String currentLine = scanner.nextLine();
            if(currentLine.contains("*/"))
            {
                String sub = currentLine.substring(currentLine.indexOf("*/"));
                int length = currentLine.substring(currentLine.indexOf("*/")).length();
                if(length != 0)
                {
                    for(int l = 0; l < characterList.size(); l++)
                    {
                        for(int q = 0; q < characterList.size(); q++)
                        {
                            if(sub.contains(characterList.get(l) + newMethod + characterList.get(q)))
                            {
                                value = true;
                                q = characterList.size();
                                l = characterList.size();
                                return value;
                            }
                            else
                            {

                            }
                        }
                    }
                }   
            }
            else if(currentLine.contains("/*"))
            {
                for(int t = 0; t < comment.size(); t++)
                {
                    if(currentLine.contains("/*"+comment.get(t)))
                    {
                        if(currentLine.contains("*/"))
                        {
                            t = comment.size();
                        }
                        else
                        {
                            String current = scanner.nextLine();
                            while(!current.contains("*/"))
                            {
                                current = scanner.nextLine();
                            }
                            t = comment.size();
                        }
                    }
                }
            }
            else if(currentLine.equals("//"))
            {
                
            }
            else if(currentLine.contains(newMethod))
            {
                for(int l = 0; l < characterList.size(); l++)
                {
                    for(int q = 0; q < characterList.size(); q++)
                    {
                        if(currentLine.contains(characterList.get(l) + newMethod + characterList.get(q)))
                        {
                            value = true;
                            q = characterList.size();
                            l = characterList.size();
                            return value;
                        }
                        else
                        {

                        }
                    }
                }
            }
            else
            {
                
            }
            i++;
        }
        //returns true if there is a methodClash, which means no refactoring is possible
        return value;
    }

}
